﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_TIME_H
#define MK_TIME_H
#include "MkUtil/MkTypes.h"
#ifdef _WIN32
#include <time.h>
int gettimeofday(struct timeval *tp, void *tzp);
#endif


class MkApi CMkTime
{
public:
    CMkTime();  //create now time
    CMkTime(Uint64 TvSec, Uint64 TvUsec);
    CMkTime(const MkString &strTime, const MkString& strSpace = "T", const MkString& strEnd = "", BOOL WithMillSec = FALSE);
    CMkTime(Uint32 Year, Uint32 Month, Uint32 Day, Uint32 Hour = 0, Uint32 Minite = 0, Uint32 Second = 0, Uint64 USecond = 0);
    ~CMkTime();

    Uint32 GetLogString(MkString &strTime) const;
    Uint32 GetString(MkString &strTime, const MkString& strSpace = "T", const MkString& strEnd = "", BOOL WithMillSec = FALSE) const;
    Uint32 GetNumberString(MkString &strTime, const MkString& strSpace = "t", const MkString& strEnd = "", BOOL WithMillSec = FALSE) const;
    Uint32 GetUnderLineString(MkString &strTime, const MkString& strSpace = "T", const MkString& strEnd = "", BOOL WithMillSec = FALSE) const;
    Uint32 GetDayString(MkString &strTime)const;
    Uint32 GetFileString(MkString &strTime)const;
    Uint32 GetGMTString(MkString& strTime)const;
    Uint32 GetMSecond() const { return static_cast<Uint32>(m_TvUsec / 1000); }
    Uint64 GetUSecond()const { return m_TvUsec; }
    Uint64 GetTvSec() const { return m_TvSec; }
    Uint64 GetNtpTime()const;
    Uint32 GetYear() const { return m_Year; }
    Uint32 GetMonth()const { return m_Month; }
    Uint32 GetDay() const { return m_Day; }
    Uint32 GetHour() const { return m_Hour; }
    Uint32 GetMinute()const { return m_Minute; }
    Uint32 GetSecond()const { return m_Second; }
    Uint32 GetWeekDay() const { return m_WeekDay; }
    BOOL IsEmpty() const;

    BOOL operator>(const CMkTime& Other)const;
    BOOL operator>=(const CMkTime& Other)const;
    BOOL operator==(const CMkTime& Other)const;
    BOOL operator<(const CMkTime& Other)const;
    BOOL operator<=(const CMkTime& Other)const;
    //offset Microseconds
    Uint64 operator-(const CMkTime& Other)const;
    CMkTime operator-(Uint64 uSec)const;
    CMkTime operator-=(Uint64 uSec);
    CMkTime operator+(Uint64 uSec)const;
    CMkTime operator+=(Uint64 uSec);
    void operator=(const CMkTime& Other);
    int OffsetHour(const CMkTime& Other)const;
    int OffsetSec(const CMkTime& Other)const;
protected:
private:
    Uint32 ParseTvSec();
    Uint32 ParseStringTime(const MkString &strTime, const MkString& strSpace = "T", const MkString& strEnd = "", BOOL WithMillSec = FALSE);
private:
    Uint64              m_TvSec;
    Uint64              m_TvUsec;   //Microseconds
    Uint32              m_Year;
    Uint32              m_Month;
    Uint32              m_Day;
    Uint32              m_Hour;
    Uint32              m_Minute;
    Uint32              m_Second;
    Uint32              m_WeekDay;
};

#define NowMkTime  CMkTime("now")

#endif
